home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 12.6 KB | 443 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Part.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef PART_H
- #include "Part.h"
- #endif
-
- #ifndef DEFINES_K
- #include "Defines.k"
- #endif
-
- #ifndef BINDING_K
- #include "Binding.k"
- #endif
-
- #ifndef FRAME_H
- #include "Frame.h"
- #endif
-
- #ifndef CONTENT_H
- #include "Content.h"
- #endif
-
- #ifndef VIEW_H
- #include "View.h"
- #endif
-
- #ifndef DIALOG_H
- #include "Dialog.h"
- #endif
-
- // ----- Framework Layer -----
-
- #ifndef FWPRTITE_H
- #include "FWPrtIte.h"
- #endif
-
- #ifndef FWABOUT_H
- #include "FWAbout.h"
- #endif
-
- #ifndef FWITERS_H
- #include "FWIters.h"
- #endif
-
- #ifndef FWIDLE_H
- #include "FWIdle.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWMENU_H
- #include "FWMenu.h"
- #endif
-
- #ifndef FWODMISC_H
- #include "FWODMisc.h"
- #endif
-
- #ifndef FWEVENT_H
- #include "FWEvent.h"
- #endif
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- #ifndef FWRESTYP_H
- #include "FWResTyp.h"
- #endif
-
- // ----- Foundation Layer -----
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
- #include <Sound.h>
- #endif
-
- //========================================================================================
- // Runtime information
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfclock
- #endif
-
- //========================================================================================
- // Globals
- //========================================================================================
-
- //========================================================================================
- // Constants
- //========================================================================================
-
- const short kClockIdleFreq = 15;
-
- //========================================================================================
- // Class CClockPart
- //========================================================================================
-
- FW_DEFINE_AUTO(CClockPart)
-
- //----------------------------------------------------------------------------------------
- // CClockPart::CClockPart
- //----------------------------------------------------------------------------------------
- CClockPart::CClockPart(ODPart* odPart) :
- FW_CPart(odPart, FW_gInstance, kPartInfoID),
- fLastTime(FW_CTime::GetCurrentTime()),
- fClockContent(NULL),
- fLockIdle(FALSE),
- fIdler(NULL),
- #ifdef FW_BUILD_MAC
- fChimeSound(NULL),
- fTickSound(NULL),
- #endif
- fUseContainerColor(FALSE)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::~CClockPart
- //----------------------------------------------------------------------------------------
- CClockPart::~CClockPart()
- {
- FW_START_DESTRUCTOR
-
- delete fIdler;
-
- #ifdef FW_BUILD_MAC
- if (fChimeSound)
- FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fChimeSound);
- fChimeSound = NULL;
-
- if (fTickSound)
- FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fTickSound);
- fTickSound = NULL;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::Initialize
- //----------------------------------------------------------------------------------------
-
- void CClockPart::Initialize(Environment* ev, ODStorageUnit* storageUnit, FW_Boolean fromStorage)
- {
- FW_CPart::Initialize(ev, storageUnit, fromStorage);
-
- // ----- Register Presentation -----
- fAnalogPresentation = RegisterPresentation(ev, kAnalogPresentation, TRUE, kAnalogClockView, kAnalogClockViewRoot);
- fDigitalPresentation = RegisterPresentation(ev, kDigitalPresentation, FALSE, kDigitalClockView, kDigitalClockViewRoot);
- fClockSettingsPresentation = RegisterPresentation(ev, kClockSettingsPresentation, false, kClockSettingsDialog, kClockSettingsDialog);
-
- // We instantiate a FW_CToggleItem from a stream so we must
- // keep it from being deadstripped.
- FW_DO_NOT_DEAD_STRIP(FW_CToggleItem);
-
- #ifdef FW_BUILD_MAC
- FW_PSharedLibraryResourceFile resFile(ev);
-
- // ----- Get default clock sounds
- fChimeSound = ::GetResource(soundListRsrc, kClockChime);
- ::DetachResource(fChimeSound);
-
- fTickSound = ::GetResource(soundListRsrc, kClockTick);
- ::DetachResource(fTickSound);
- #endif
-
- // ----- Create the Idler -----
- fIdler = FW_NEW(FW_CIdler, (this, kClockIdleFreq));
- fIdler->RegisterIdle(ev);
- }
-
- //------------------------------------------------------------------------------
- // CClockPart::NewPartContent
- //------------------------------------------------------------------------------
-
- FW_CContent* CClockPart::NewPartContent(Environment* ev)
- {
- fClockContent = FW_NEW(CClockContent, (ev, this));
- return fClockContent;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::NewFrame
- //----------------------------------------------------------------------------------------
-
- FW_CFrame* CClockPart::NewFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation, FW_Boolean fromStorage)
- {
- FW_UNUSED(fromStorage);
-
- if (presentation == fClockSettingsPresentation)
- {
- return FW_NEW(CClockSettingsDialogFrame, (ev, odFrame, presentation, this));
- }
-
- return FW_NEW(CClockFrame, (ev, odFrame, presentation, this, fClockContent));
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::Tick
- //----------------------------------------------------------------------------------------
-
- short CClockPart::Tick(Environment* ev, FW_CPresentation* presentation, const FW_CTime& currentTime)
- {
- short frameCount = 0;
-
- // Update all the display frames of the same type
- FW_CPresentationFrameIterator iter(ev, presentation);
- for (CClockFrame *clockFrame = (CClockFrame*)iter.First(ev); iter.IsNotComplete(ev); clockFrame = (CClockFrame*)iter.Next(ev))
- {
- if ((!clockFrame->IsInLimbo(ev)))
- {
- frameCount++;
- if (clockFrame->GetViewType(ev) == FW_CPart::fgViewAsFrameToken)
- {
- CClockView* clockView = clockFrame->GetClockView(ev);
- if (clockView)
- clockView->UpdateClock(ev, currentTime);
- }
- }
- }
-
- return frameCount;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::DoIdle
- //----------------------------------------------------------------------------------------
-
- FW_Handled CClockPart::DoIdle(Environment* ev, const FW_CNullEvent& theNullEvent)
- {
- FW_UNUSED(theNullEvent);
-
- if (fLockIdle)
- return FW_kHandled;
-
- FW_CTime currentTime = FW_CTime::GetCurrentTime() + fClockContent->GetTimeOffset();
- if (fLastTime != currentTime)
- {
- short frameCount = 0;
-
- frameCount = Tick(ev, fAnalogPresentation, currentTime);
- frameCount += Tick(ev, fDigitalPresentation, currentTime);
-
- if (frameCount > 0)
- {
- if (fClockContent->HasTickSound())
- this->PlayTickSound();
- if (fClockContent->HasChimeSound() && (currentTime.GetMinute() == 0) && (currentTime.GetSecond() == 0))
- this->PlayChimeSound();
- }
-
- fLastTime = currentTime;
- }
- return FW_kHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::DoAbout
- //----------------------------------------------------------------------------------------
-
- FW_Handled CClockPart::DoAbout(Environment* ev)
- {
- ::FW_About(ev, this, kAbout);
-
- return FW_kHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::SynchContainingPartProperties
- //----------------------------------------------------------------------------------------
-
- void CClockPart::SynchContainingPartProperties(Environment* ev, FW_CPresentation* presentation)
- {
- FW_CPresentationFrameIterator iter(ev, presentation);
- for (CClockFrame* frame = (CClockFrame*)iter.First(ev); iter.IsNotComplete(ev); frame = (CClockFrame*)iter.Next(ev))
- {
- frame->SynchContainingPartProperties(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::DoMenu
- //----------------------------------------------------------------------------------------
-
- FW_Handled CClockPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
- {
- FW_Handled menuHandled = FW_kHandled;
-
- switch (theMenuEvent.GetCommandID(ev))
- {
- case cClockSoundsTick:
- fClockContent->ToggleTickSound();
- this->Changed(ev);
- break;
-
- case cClockSoundsChime:
- fClockContent->ToggleChimeSound();
- this->Changed(ev);
- break;
-
- case cClockSettings:
- OpenClockSettingsDialog(ev);
- break;
-
- case cClockUseContainerColor:
- {
- fUseContainerColor = !fUseContainerColor;
- SynchContainingPartProperties(ev, fAnalogPresentation);
- SynchContainingPartProperties(ev, fDigitalPresentation);
- }
- break;
-
- default:
- menuHandled = FW_kNotHandled;
- }
-
- return menuHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::DoAdjustMenus
- //----------------------------------------------------------------------------------------
-
- FW_Handled CClockPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
- {
- if (hasMenuFocus)
- {
- menuBar->EnableAndCheckCommand(ev, cClockSoundsTick, TRUE, fClockContent->HasTickSound());
- menuBar->EnableAndCheckCommand(ev, cClockSoundsChime, TRUE, fClockContent->HasChimeSound());
-
- menuBar->EnableCommand(ev, cClockSettings, true);
-
- menuBar->EnableAndCheckCommand(ev, cClockUseContainerColor, !isRoot, fUseContainerColor);
- }
-
- return FW_kNotHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::PlayTickSound
- //----------------------------------------------------------------------------------------
-
- void CClockPart::PlayTickSound()
- {
- #ifdef FW_BUILD_MAC
- ::SndPlay(NULL, (SndListHandle)fTickSound, TRUE);
- #endif
- #ifdef FW_BUILD_WIN
- ::MessageBeep(-1);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::PlayChimeSound
- //----------------------------------------------------------------------------------------
-
- void CClockPart::PlayChimeSound()
- {
- #ifdef FW_BUILD_MAC
- ::SndPlay(NULL, (SndListHandle)fChimeSound, TRUE);
- #endif
- #ifdef FW_BUILD_WIN
- ::MessageBeep(-1);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::SwitchPresentation
- //----------------------------------------------------------------------------------------
-
- FW_CPresentation* CClockPart::SwitchPresentation(FW_CPresentation* currentPresentation) const
- {
- if (currentPresentation == fAnalogPresentation)
- return fDigitalPresentation;
- else
- return fAnalogPresentation;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::OpenClockSettingsDialog
- //----------------------------------------------------------------------------------------
-
- void CClockPart::OpenClockSettingsDialog(Environment* ev)
- {
- FW_CPoint position(FW_kZeroPoint);
- FW_CPoint size(FW_IntToFixed(320), FW_IntToFixed(130));
- FW_WindowStyle style = FW_kStandardDialogPosition | FW_kHasCaption;
-
- FW_CString title;
- FW_PSharedLibraryResourceFile resFile(ev);
- ::FW_LoadStringByID(ev, resFile, kClockStrings, FW_kMultiStringRes, kClockSettingsTitleString, title);
-
- CClockSettingsDialogFrame* dialog = (CClockSettingsDialogFrame*) FW_CDialogFrame::NewAndShowModalDialog(ev,
- this, // Your part
- fClockSettingsPresentation, // Used in NewFrame
- size, // Window size
- position, // Window position
- style, // Make dialog moveable
- title); // Title for moveable dialog
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::SetClock
- //----------------------------------------------------------------------------------------
-
- void CClockPart::SetClock(Environment* ev, long hourOffset, const FW_CString& faceString)
- {
- // FW_CTimeSpan(long days, short hours, short minutes, short seconds);
- FW_CTimeSpan offset(0, hourOffset, 0, 0);
- fClockContent->SetTimeOffset(offset);
- fClockContent->SetFaceString(faceString);
-
- // Notify interested parties of change
- this->Changed(ev);
- fAnalogPresentation->ContentUpdated(ev);
- fDigitalPresentation->ContentUpdated(ev);
-
- // Redraw the analog clock
- fAnalogPresentation->Invalidate(ev);
-
- // Don't need to redraw the digital clock since it will update in a second
- // fDigitalPresentation->Invalidate(ev);
- }
-